<?php

namespace App\Repository\Repositories;

use App\Repository\Models\ProductBrands;
use App\Repository\Repositories\Interfaces\ProductBrandsRepository;

/**
 * Class ProductBrandsRepositoryEloquent.
 *
 * @package namespace App\Repository\Repositories;
 */
class ProductBrandsRepositoryEloquent extends BaseRepository implements ProductBrandsRepository
{
    /**
     * 搜索
     * @var array
     */
    protected $fieldSearchable = [
        'name'=> 'like',
        'copy_category'=>'like',
        'created_at'=>'between',
        'id'=> 'in',
    ];

    /**
     * Specify Model class name
     *
     * @return string
     */
    public function model()
    {
        return ProductBrands::class;
    }

    public function createNew($data)
    {
        $categories = isset($data['categories']) ? $data['categories'] : "";
        if($categories){
            unset($data['categories']);
            $data['copy_category'] = implode(",", $categories);
        }
        $model = $this->model->newInstance($data);
        $model->save();
        if($categories){
            $model->categories()->attach($categories);
        }
        $ret['data']=$model->toArray();
        return $ret;   
    }
    
}
